package com.dpj.wait_test;

import lombok.extern.slf4j.Slf4j;

@Slf4j(topic = "c.TestCorrectPostureStep3")
public class TestCorrectPostureStep3 {
    static final Object room = new Object();
    static boolean hasCigarette = false; //有没有烟
    static boolean hasTakeout = false;//外卖到了没
//虚假唤醒，notify的时候唤醒了没有用的线程
    public static void main(String[] args) throws InterruptedException {
        new Thread(() -> {
            synchronized (room) {
                log.debug("有烟没？[{}]", hasCigarette);
                if (!hasCigarette) {
                    log.debug("没烟,先歇会儿!");
                    try {
                        room.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                log.debug("有烟没？[{}]", hasCigarette);
                if (hasCigarette) {
                    log.debug("有烟,可以干活了!");
                }
            }
        },"小南").start();

        new Thread(() -> {
            synchronized (room) {
                log.debug("外卖到了？[{}]", hasTakeout);
                if (!hasTakeout) {
                    log.debug("没外卖,先歇会儿!");
                    try {
                        room.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                log.debug("外卖到了没？[{}]", hasTakeout);
                if (hasTakeout) {
                    log.debug("外卖到了,可以干活了!");
                }
            }
        },"小女").start();
        Thread.sleep(1000);
        new Thread(()->{
            synchronized (room){
                hasTakeout=true;
                log.debug("外卖到了了哦");
                room.notifyAll();
            }

        },"送外卖的").start();
    }
}
